**Combine datasets
use "/Users/survey.dta"
recode oslaua_protect code 
merge m:m code using "/Users/census_cps.dta" 
drop if _merge==2
drop _merge

merge m:1 code using "/Users/code_area.dta"
drop if _merge==2
drop _merge

merge m:1 Area using "/Users/finalpub.dta"
drop _merge

merge m:1 Area using  "/Users/nbpubs.dta"
drop _merge

rename code ID_code
merge m:1 ID_code using "/Users/imd.dta"


**Ratio/pubs per year
gen pub_community=.
replace pub_community = howmany2013community/pubs if year==2013
replace pub_community = howmany2014community/pubs if year==2014
replace pub_community = howmany2015community/pubs if year==2015
replace pub_community = howmany2016community/pubs if year==2016

gen pub_citycentre =.
replace pub_citycentre = howmany2013citycentre/pubs if year==2013
replace pub_citycentre = howmany2014citycentre/pubs if year==2014
replace pub_citycentre = howmany2015citycentre/pubs if year==2015
replace pub_citycentre = howmany2016citycentre/pubs if year==2016

gen pub_gastropub =.
replace pub_gastropub = howmany2013independent/pubs if year==2013
replace pub_gastropub = howmany2014independent/pubs if year==2014
replace pub_gastropub = howmany2015independent/pubs if year==2015
replace pub_gastropub = howmany2016independent/pubs if year==2016

gen pub_chain=.
replace pub_chain = howmany2013chain/pubs if year==2013
replace pub_chain= howmany2014chain/pubs if year==2014
replace pub_chain= howmany2015chain/pubs if year==2015
replace pub_chain = howmany2016chain/pubs if year==2016

sort pid year
quietly by pid year:  gen dup = cond(_N==1,0,_n)
sum dup
tab dup if dup==0
drop if dup==0

gen log= log(pop)

*** Analysis

**Figure 1
profileplot howmany2003 howmany2004 howmany2005 howmany2006 howmany2007 howmany2008 howmany2009 howmany2010 howmany2011 howmany2012 howmany2013 howmany2014 howmany2015, by (region) xlabel(1 "2003" 2 "2004" 3 "2005" 4 "2006" 5 "2007" 6 "2008" 7 "2009" 8 " 2010" 9 "2011" 10 "2012" 11 "2013" 12 "2014" 13"2015")  

**Table 1
gen year1= wave*ID_code
encode region, gen(region1)
xtset pidp year

xtlogit support i.gender i.ethnicity  i.education i.job_3 age i.year i.wave i.region1 x_indinub_lw c.pub_community, i(pidp)  vce(cluster ID_code)
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Region FE, YES) replace

xtmelogit support i.gender i.education i.job_3 age i.year x_indinub_lw c.pub_community i.ethnicity || year1:
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

xtlogit support i.gender i.ethnicity  i.education i.job_3 age i.year i.wave i.region1 x_indinub_lw c.pub_community EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log, i(pidp)  vce(cluster ID_code) 
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

xtmelogit support pub_community i.gender i.ethnicity i.education i.job_3 age i.wave EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw  ||year1:
outreg2 using Table1.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

// to obtain percentage points (result *100)
margins , dydx(pub_community)  atmeans

**Interaction Effects_Individual (Table 2)
recode race (1=0) (-9=.) ( 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 97=1), gen(ethnicity1)
recode job_3 (1=3) (2=2) (3=1), gen(job1)

xtlogit support c.pub_community##i.ethnicity1 i.education i.gender age  i.wave EU_Migrantgrowth NONEU_Migrantgrowth  umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw i.year, i(pidp) vce(cluster ID_code)
outreg2 using Table2.xls, nolabel bracket bdec(3) addtext(Region FE, YES) replace

xtlogit support c.pub_community##i.job1 i.ethnicity i.education i.gender age  i.wave EU_Migrantgrowth NONEU_Migrantgrowth  umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw i.year, i(pidp) vce(cluster ID_code)
outreg2 using Table2.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

**Coefplots_Individual (Figures 3 and 4)
xtlogit support c.pub_community##i.ethnicity1 i.education i.gender age  i.wave EU_Migrantgrowth NONEU_Migrantgrowth  umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw i.year, i(pidp) vce(cluster ID_code)
coefplot, xline(0)  keep(pub_community 1.ethnicity1#c.pub_community ) coeflabels(pub_community= "Closures of Community Pubs" 1.ethnicity1#c.pub_community ="White British Population X Closure of Community Pubs")
graph save Graph "/Users/interaction1.gph"

xtlogit support c.pub_community##i.job1 i.ethnicity i.education i.gender age  i.wave EU_Migrantgrowth NONEU_Migrantgrowth  umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw i.year, i(pidp) vce(cluster ID_code)
coefplot, xline(0)  keep(1.job1#c.pub_community 2.job1#c.pub_community 3.job1#c.pub_community ) coeflabels(2.job1#c.pub_community ="Intermediate Jobs X Closure of Community Pubs" 3.job1#c.pub_community ="Routine Jobs X Closure of Community Pubs")
graph save Graph "/Users/interaction2.gph"

graph combine "/Users/interaction1.gph" "/Users/interaction2.gph" 

**Interaction Effects_Context (Table 3)
xtlogit support i.gender i.ethnicity  i.education age i.year i.wave i.region1 x_indinub_lw c.pub_community##c.EU_Migrantgrowth  NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log, i(pidp)  vce(cluster ID_code) 
outreg2 using Table3.xls, nolabel bracket bdec(3) addtext(Region FE, YES) replace

xtlogit support i.gender i.ethnicity  i.education  age i.year i.wave i.region1 x_indinub_lw c.pub_community##c.NONEU_Migrantgrowth  EU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log, i(pidp)  vce(cluster ID_code) 
outreg2 using Table3.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

xtlogit support i.gender i.ethnicity i.education age i.year i.wave i.region1 x_indinub_lw c.pub_community##c.umemployment_rate_aps c.NONEU_Migrantgrowth  EU_Migrantgrowth ResidentAge60plusshare  imdaveragescore  shareofstudents log, i(pidp)  vce(cluster ID_code) 
outreg2 using Table3.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

xtlogit support i.gender i.ethnicity i.education age i.year i.wave i.region1 x_indinub_lw c.pub_community##c.imdaveragescore NONEU_Migrantgrowth  EU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps shareofstudents log, i(pidp)  vce(cluster ID_code) 
outreg2 using Table3.xls, nolabel bracket bdec(3) addtext(Region FE, YES) append

**Marginal Effects_Context (Figure 5)
xtlogit support c.pub_community##c.umemployment_rate_aps i.job_3 i.gender i.ethnicity i.education i.year i.wave i.region1 EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare imdaveragescore shareofstudents log x_indinub_lw, i(pidp) vce(cluster ID_code)
quietly margins , dydx(pub_community)  at(umemployment_rate_aps=(0(1)15))
marginsplot, recast(line) recastci(rarea) ytitle(UKIP Support) ytitle(, size(small)) xtitle(Unemployment Rate) xtitle(, size(small)) title("", nobox) legend(off) addplot(scatter where umemployment_rate_aps, xaxis(2) ms(none) mlabel(pipe) mlabsize(small) mlabcolor(black) mlabpos(0.5) legend(off) )
graph save Graph "/Users/interaction3.gph"

xtlogit support i.gender i.ethnicity i.job_3  i.education age i.year i.wave i.region1 x_indinub_lw c.pub_community##c.imdaveragescore NONEU_Migrantgrowth  EU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps shareofstudents log, i(pidp)  vce(cluster ID_code) 
quietly margins , dydx(pub_community)  at(imdaveragescore=(0(5)42))
marginsplot, recast(line) recastci(rarea) ytitle(UKIP Support) ytitle(, size(small)) xtitle(IMD) xtitle(, size(small)) title("", nobox) legend(off) addplot(scatter where imdaveragescore, xaxis(2) ms(none) mlabel(pipe) mlabsize(small) mlabcolor(black) mlabpos(0.5) legend(off) )
graph save Graph "/Users/interaction4.gph"


***Robustness Checks
**Table 4
xtlogit support pub_chain i.gender i.ethnicity age i.education i.job_3 i.year i.wave EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps shareofstudents log imdaveragescore x_indinub_lw  i.region1 ,i(pidp) vce(cluster ID_code)
outreg2 using Table4.xls, nolabel bracket bdec(3) addtext(Region FE, YES) replace

xtlogit support pub_citycentre i.gender i.ethnicity age i.job_3 i.education i.year i.wave  EU_Migrantgrowth NONEU_Migrantgrowth  ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw  i.region1 , i(pidp) vce(cluster ID_code)
outreg2 using Table4.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

xtlogit support pub_gastropub i.gender i.ethnicity age i.job_3 i.education i.year i.wave EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log x_indinub_lw  i.region1, i(pidp) vce(cluster ID_code)
outreg2 using Table4.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

xtlogit support pub_community i.gender i.ethnicity age i.job_3 i.education i.year i.wave ethnic_groups NONEU_Migrantgrowth   EU_2001Migrantshare umemployment_rate_aps TotalImpactFLWAAYR eufundspercapitapayment13 NONEU_2001Migrantshare ResidentAge60plusshare unqualifiedsharechange imdaveragescore crimeaveragescore barrierstohousingandservicesaver livingenvironmentaveragescore incomedeprivationaffectingolderp healthdeprivationanddisabilityav log shareofstudents x_indinub_lw  i.region1 , i(pidp) vce(cluster ID_code)
outreg2 using Table4.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

**Table 5
use "/Users/census.dta", replace
drop _merge

merge m:1 Area using "/Users/finalpub.dta"
drop _merge

merge m:1 Area using  "/Users/nbpubs.dta"
drop _merge

gen five_years = lastfivecommunity/pubs
gen ten_years = lasttencommunity/pubs
gen fifteen_years = lastfifteencommunity/pubs

encode Region, gen(region)
xtset region

xtreg MEP_UKIPPct five_years ResidentAge60plusshare EU_2001Migrantshare unqualifiedsharechange umemployment_rate_aps Total_EconomyEU_dependence noqual_2001_sh ManufacturingShareGrowth, fe cluster (id) nonest
outreg2 using Table5.xls, nolabel bracket bdec(3) addtext(Country FE, YES) replace

xtreg MEP_UKIPPct ten_years ResidentAge60plusshare EU_2001Migrantshare unqualifiedsharechange umemployment_rate_aps Total_EconomyEU_dependence noqual_2001_sh ManufacturingShareGrowth, fe cluster (id) nonest
outreg2 using Table5.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

xtreg MEP_UKIPPct fifteen_years ResidentAge60plusshare EU_2001Migrantshare unqualifiedsharechange umemployment_rate_aps Total_EconomyEU_dependence noqual_2001_sh ManufacturingShareGrowth, fe cluster (id) nonest
outreg2 using Table5.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

***Alternative Dependent Variables (Table 6)
*With BNP
xtlogit support1 pub_community i.gender i.ethnicity i.education i.year age i.job_3 i.wave  x_indinub_lw  i.region1 , i(pidp) vce(cluster ID_code)
outreg2 using Table6.xls, nolabel bracket bdec(3) addtext(Country FE, YES) replace

*With Brexit
xtlogit support pub_community i.gender i.ethnicity i.education i.year age i.job_3 i.wave Pct_Leave  x_indinub_lw  i.region1 , i(pidp) vce(cluster ID_code)
outreg2 using Table6.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

*Excluding London
drop if rr_3==1
xtlogit support pub_community i.gender i.ethnicity i.education i.year age i.job_3 i.wave  EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents log  x_indinub_lw  i.region1 , i(pidp) vce(cluster ID_code)
outreg2 using Table6.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

**Other Parties (Table 7)
**With Labour instead of UKIP 
logit support_Labour pub_community i.gender age i.job_3 i.wave i.ethnicity i.education i.year EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents  log i.region1 x_indinub_lw,  vce(cluster ID_code) 
outreg2 using Table7.xls, nolabel bracket bdec(3) addtext(Country FE, YES) replace

**With Conservative instead of UKIP 
logit support_Cons pub_community i.gender  age i.job_3 i.wav i.ethnicity i.education i.year EU_Migrantgrowth NONEU_Migrantgrowth ResidentAge60plusshare umemployment_rate_aps imdaveragescore shareofstudents  log i.region1 x_indinub_lw, vce(cluster ID_code) 
outreg2 using Table7.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

*Abstention rate
xtlogit abstention pub_community i.gender age i.ethnicity i.education i.year i.wave x_indinub_lw  i.region1 , i(pidp)  vce(cluster ID_code) 
outreg2 using Table7.xls, nolabel bracket bdec(3) addtext(Country FE, YES) append

